
package com.huaweicloud.reqmt.function;

import static com.huaweicloud.reqmt.utils.HoildayCheckUtil.getTimeList;
import static com.huaweicloud.reqmt.utils.HoildayCheckUtil.judgeDateDuration;
import static com.huaweicloud.reqmt.utils.HoildayCheckUtil.sortOutHoilday;

import com.huaweicloud.reqmt.domain.dws_haydn_self_verification_dura_reqmt_d;

import org.apache.flink.api.common.functions.MapFunction;

import java.util.List;

/**
 * 功能描述
 *
 * @since 2022-11-14
 */

public class DwsMapFunction
    implements MapFunction<dws_haydn_self_verification_dura_reqmt_d, dws_haydn_self_verification_dura_reqmt_d> {
    @Override
    public dws_haydn_self_verification_dura_reqmt_d map(dws_haydn_self_verification_dura_reqmt_d unit)
        throws Exception {
        // 有开始时间和结束时间
        if (unit.getStartTime() != null && unit.getEndTime() != null) {
            String startTime = unit.getStartTime();
            String endTime = unit.getEndTime();
            // 开始时间与结束时间不是同一天
            if (!startTime.substring(0, 10).equals(endTime.substring(0, 10))) {
                // 建立开始时间与结束时间的日期List
                List timeList = getTimeList(startTime, endTime);
                // 判断时间List中有多少的节假日, 并剔除
                List workdayList = sortOutHoilday(timeList);
                Long startTimeDura = judgeDateDuration(startTime, true);
                Long endTimeDura = judgeDateDuration(endTime, false);
                // 判断剩余时间List的size, 确认开始时间到结束时间的工作日
                Long workDuration = (long) workdayList.size() * 24 * 60 * 60 + startTimeDura + endTimeDura;
                unit.setDuraSeconds(String.valueOf(workDuration));
                timeList.clear();
                workdayList.clear();
            } else {
                // 开始时间与结束时间是同一天
                Long workDuration = judgeDateDuration(startTime, true) - judgeDateDuration(endTime, true);
                // 不到1秒按照1秒算
                workDuration = workDuration == 0 ? 1 : workDuration;
                unit.setDuraSeconds(String.valueOf(workDuration));
            }
        }
        return unit;
    }
}